package cn.jbolt.ec.util;

import cn.jbolt.common.model.*;
import com.jfinal.kit.Kv;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MyUtil {

    public static final MyUtil me = new MyUtil();



    /**************************************
     * @author  zedomi
     ***********************************
     * /

    /**
     * 通过commodity_typeId 获取上级的信息
     * @param Commodity_typeId
     * @return
     */
    public BecRetailerCommodityType get_Commodity_type(Integer Commodity_typeId) {
        if (Commodity_typeId == null || Commodity_typeId <= 0) {
            return null;
        }
        BecRetailerCommodityType becRetailerCommodityType = new BecRetailerCommodityType();
        BecRetailerCommodityType becRetailerCommodityType1 = new BecRetailerCommodityType();
        becRetailerCommodityType1=becRetailerCommodityType.dao().findById(Commodity_typeId);
        if (becRetailerCommodityType1==null)
        {
            return null;
        }
        else {
            return becRetailerCommodityType.dao().findById(Commodity_typeId);
        }


    }


    /**
     *获取上级及上上级的名称
     * @param Commodity_typeid
     * @return
     */
    public String get_commodity_full_name(Integer Commodity_typeid) {
        String backfullname = "";







        BecRetailerCommodityType becRetailerCommodityType = get_Commodity_type(Commodity_typeid);
        if (becRetailerCommodityType==null){
            return "此商品类型不存在";
        }

        if (becRetailerCommodityType==null)return null;
        backfullname = becRetailerCommodityType.getName();
        while (true) {
            if (becRetailerCommodityType.getPid() == 0) {
               
                break;
            }

            becRetailerCommodityType = get_Commodity_type(becRetailerCommodityType.getPid());
            if (becRetailerCommodityType==null){
                return "此商品类型不存在";
            }
            backfullname = becRetailerCommodityType.getName() + "_" + backfullname;


        }
        return backfullname;


    }
    /**
     * 获取顶级的商品类型
     */
    public Integer get_top_commodity_type(Integer commodity_type_id){
        Integer commodity_top_type_id=0;
        BecRetailerCommodityType becRetailerCommodityType =  get_Commodity_type(commodity_type_id);
        if (becRetailerCommodityType==null) return null;
        while(true){
            if (becRetailerCommodityType.getPid()==0){
                return becRetailerCommodityType.getId();
            }
            becRetailerCommodityType = get_Commodity_type(becRetailerCommodityType.getPid());
        }

    }

    /**
     * 通过商品id获取商品详情
     * @param Commdoity_id
     * @return
     */
    public List<BecRetailerCommodityOption> get_commodity_option(Integer Commdoity_id){
        Kv kv = Kv.by("commodity_id",Commdoity_id);
        BecRetailerCommodityOption becRetailerCommodityOption1 = new BecRetailerCommodityOption();
        List<BecRetailerCommodityOption> becRetailerCommodityOption = becRetailerCommodityOption1.dao().template("ec.mall.manage.commodity.select_option",kv).find();
        return becRetailerCommodityOption;
    }

    /**
     * 修改积分对象表转化为文字
     */
    public String get_log_tbnameString(String tbName){
        if (tbName == null)
        {
            return null;
        }
        String tbanameString = "";
        switch (tbName){
            case "bec_member":tbanameString="会员";
            default:;
        }
        return tbanameString;
    }

    /**
     * 获取修改的管理员
     */
    public String get_log_user(Integer id){
        if (id==null){
            return null;
        }
        User user1 = new User();
        User user = user1.dao().findById(id);
        if (user==null){
            return null;
        }
        return  user.getName();
    }

    /**
     * 改变类型
     */
    public String get_change_type_name(Integer change_type){
        String type_name[]={"购买",
                "进货",
                "核销",
                "特价库存变化(增加)",
                "特价库存变化(减少)",
                "特价购买",
                "积分秒杀商品库存变化(增加)",
                "积分秒杀商品库存变化(减少)",
                "全民秒杀商品库存变化(增加)",
                "全民秒杀商品库存变化(减少)"
        };
        if (change_type!=null && change_type<=type_name.length&&change_type>0){
            return type_name[change_type-1];
        }
        return null;
    }



    /**
     * 分销商状态转化
     */
    public String get_spreader_audited(Integer audited){
        String change_audited[]={"已提交",
                "已通过",
                "未通过"
        };
        if (audited!=null && audited<change_audited.length&&audited>=0){
            return change_audited[audited];
        }
        else return null;

    }

    /**
     * 转化是否特价商品
     */
    public String change_special_commodity(Integer status){
        String change[]={"否",
                "是",
                "待审核"
        };
        if (status!=null && status<change.length && status>=0)
            return change[status];
        return null;

    }


    /*************************************
     * @author heqingyu
     * ************************************
     */

    /**
     * 通过Typeid获得BecRetailerType数据
     *
     * @param Typeid
     * @return
     */
    public BecRetailerType get_retailerType(Integer Typeid) {
        if (Typeid == null || Typeid <= 0) {
            return null;
        }
        BecRetailerType becRetailerType = new BecRetailerType();
        if (becRetailerType.dao().findById(Typeid)==null){
            return null;
        }
        return becRetailerType.dao().findById(Typeid);

    }


    /**
     * 通过TypeId获取retailer商家类型的名称
     * @param Typeid
     * @return
     */
    public String get_retailer_type_name(Integer Typeid){
        BecRetailerType becRetailerType=get_retailerType(Typeid);
        return becRetailerType==null?"":becRetailerType.getName();
    }

    /**
     * 缓存通过clientId获得BecMember数据
     *
     * @param clientId
     * @return
     */
    public BecMember get_member(Integer clientId) {
        if (clientId == null || clientId <= 0) {
            return null;
        }
        BecMember becMember = new BecMember();
        if (becMember.dao().findById(clientId)==null){
            return null;
        }
        return becMember.dao().findById(clientId);

    }

    /**
     * 通过clientId中获取member商家联系人的名字
     * @param clientId
     * @return
     */
    public String get_retailer_order_memberName(Integer clientId){
        BecMember becMember = get_member(clientId);
        return becMember==null?"":becMember.getTitle();
    }


    /**
     * 通过retailer_id获得BecRetailer数据
     * @param retailer_id
     * @return
     */
    public BecRetailer get_retailer(Integer retailer_id) {
        if (retailer_id == null || retailer_id <= 0) {
            return null;
        }
        BecRetailer becRetailer = new BecRetailer();
        if (becRetailer.dao().findById(retailer_id)==null){
            return null;
        }
        return becRetailer.dao().findById(retailer_id);

    }

    /**
     * 通过retailer_id获取retailer商家名称
     * @param retailer_id
     * @return
     */
    public String get_retailer_name(Integer retailer_id){
        BecRetailer becRetailer = get_retailer(retailer_id);
        return becRetailer==null?"":becRetailer.getName();
    }


    /**
     * 把时间Long型转化为Date型
     * @param Time
     * @return
     */
    public String get_date(Long Time){
        if (Time == null)
            return null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(Time);
        return df.format(date);

    }

    /**
     * 把时间Long型转化为 只有时间的 Date型
     * @param Time
     * @return
     */
    public String get_date_daytime(Long Time){
        if (Time == null)
            return null;
        SimpleDateFormat df = new SimpleDateFormat(" HH:mm:ss");
        Date date = new Date(Time);
        return df.format(date);

    }


    /**
     * 将 Sting 类型  换成 date类型
     * @param time
     * @return
     */
    public Date String_to_date(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(" HH:mm:ss");
        Date dateTime = null;


        try {
            dateTime = simpleDateFormat.parse(time);
            System.out.println("???!!!!!"+dateTime);

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(dateTime);
            calendar.set(2020,0,01);
            System.out.println("aaaaaaaa"+calendar);


            return calendar.getTime();

        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *
     * @param status
     * @return
     */
    public String get_retailer_status(Integer status){
        if (status == null)
            return null;
        String status_string[]={"审核通过",
                "删除",
                "待审核",
                "被驳回"
        };
        if (status >=0&&status < status_string.length){
           return status_string[status];
        }
        return null;


    }



    /**
     * 把订单状态由数字转化成文字
     * @param status
     * @return
     */
    public String get_retailer_order_statusString(Integer status){
        if (status == null)
            return null;
        String statusString=null;
        switch (status){
            case 5: statusString="未结账";break;
            case 6: statusString="已结账未支付";break;
            case 7: statusString="已余额支付";break;
            case 8: statusString="已微信支付尾款";break;
            case 9: statusString="积分支付";break;
            case 14:statusString="退款开始";break;
            case 15:statusString="拒绝退款";break;
            case 16:statusString="退款成功";break;
            case 18: statusString="结单";break;
            case 20: statusString="废单";break;
            default:;
        }
        return statusString;
    }

    /**
     * 把订单打印状态转换为文字
     * @param print_status
     * @return
     */
    /**
     * 把订单打印状态转换为文字
     * @param print_status
     * @return
     */
    public String get_retailer_order_printStatusString(Integer print_status) {
        if (print_status == null)
            return null;
        String printString[] = {"初始化","已发起调用","已打印"};
        if (print_status>=0&&print_status<printString.length){
            return printString[print_status];
        }
        else return null;

    }



    /**
     * 把订单字符状态转换为文字
     * @param pay_type
     * @return
     */
    public String get_retailer_order_payTypeString(Integer pay_type){
        if (pay_type == null)
            return null;
        String payTypeString[] = {"微信",
                    "平台余额",
                    "商家余额"};
        if (pay_type>0&&pay_type<=payTypeString.length){
            return payTypeString[pay_type-1];
        }
        else
            return null;


    }



    public String get_order_type_string(Integer type){
        if (type == null)
            return null;
        String type_string = "";
        switch (type){
            case 1:type_string = "正常";break;
            case 2:type_string = "简易";break;
            case 3:type_string = "商家余额充值送卷";break;
            case 5:type_string = "平台余额充值";break;
            case 14:type_string = "全民秒杀";break;
            case 15:type_string = "分销商秒杀";break;
            default:;
        }
        return type_string;
    }




    /**
     * 把 价格 分 转换成  元
     * @param price
     * @return
     */
    public String get_price_string(Long price){
        if (price == null)
            return  null;
        Long prefix = price/100;
        String prefix_string = prefix.toString();
        Long suffix1 = (price/10)%10;
        String suffix1_string = suffix1.toString();
        Long suffix2 = price%10;
        String suffix2_string = suffix2.toString();

        if (prefix_string == null || suffix1_string ==null || suffix2_string ==null)
            return null;
        return prefix_string+"."+suffix1_string+suffix2_string;
    }

    public String get_price_string(Integer price){
        if (price == null)
            return  null;
        Integer prefix = price/100;
        String prefix_string = prefix.toString();
        Integer suffix1 = (price/10)%10;
        String suffix1_string = suffix1.toString();
        Integer suffix2 = price%10;
        String suffix2_string = suffix2.toString();

        if (prefix_string == null || suffix1_string ==null || suffix2_string ==null)
            return null;
        return prefix_string+"."+suffix1_string+suffix2_string;
    }


    public BecRetailerOrderCommodity get_order_commondity_by_orderid(Integer order_id){
        BecRetailerOrderCommodity becRetailerOrderCommodity_dao = new BecRetailerOrderCommodity().dao();
        BecRetailerOrderCommodity becRetailerOrderCommodity = becRetailerOrderCommodity_dao.findFirst("select * from bec_retailer_order_commodity where order_id = ?",order_id);
        if (becRetailerOrderCommodity == null)
            return null;
        return becRetailerOrderCommodity;
    }


    public List<BecRetailerOrderCommodity> get_order_commodity_list(Integer order_id){
        BecRetailerOrderCommodity becRetailerOrderCommodity_dao = new BecRetailerOrderCommodity().dao();
        List<BecRetailerOrderCommodity> becRetailerOrderCommodity = becRetailerOrderCommodity_dao.find("select * from bec_retailer_order_commodity where order_id = ?",order_id);
        if (becRetailerOrderCommodity == null || becRetailerOrderCommodity.size() == 0)
            return null;
        return becRetailerOrderCommodity;

    }

    public String get_order_commodity_name_odercommodity(Integer order_id){
        BecRetailerOrderCommodity becRetailerOrderCommodity = get_order_commondity_by_orderid(order_id);

        return becRetailerOrderCommodity==null?"":becRetailerOrderCommodity.getCommodityName();

    }

    public String get_order_option_title_odercommodity(Integer order_id){
        BecRetailerOrderCommodity becRetailerOrderCommodity = get_order_commondity_by_orderid(order_id);
        return becRetailerOrderCommodity==null?"":becRetailerOrderCommodity.getOptionTitle();

    }



    public String get_order_order_id(Integer order_id){
        if (order_id == null)
            return null;
        BecRetailerOrder becRetailerOrder_dao = new BecRetailerOrder().dao();
        BecRetailerOrder becRetailerOrder = becRetailerOrder_dao.findById(order_id);
        return becRetailerOrder==null?"":becRetailerOrder.getOrderId();
    }



}
